package org.grpc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.grpc.annotation.Monitor;
import org.grpc.userInfo.UserInfoServiceGrpc;
import org.grpc.userInfo.UserStr;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ExecutionException;

/**
 * description:
 *
 * @author shun.chen
 * @date 2022/5/10
 */
@Api(value = "测试链路监控接口")
@Slf4j
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @GrpcClient("grpc-server")
    UserInfoServiceGrpc.UserInfoServiceFutureStub userInfoServiceStub;

    @ApiOperation("链路监控http")
    @GetMapping(value = "/query/simple")
    public String querySimple(String param) {
        log.info("querySimple 入参：{} ", param);
        return "querySimple response " + param;
    }

    @Monitor(description = "/queryUser")
    @ApiOperation("链路监控rpc")
    @GetMapping(value = "/query/{id}")
    public String queryUser(@PathVariable Integer id, String str) {
        String userResp = null;
        try {
            System.out.println("queryUser =======> " + str);
            userResp = userInfoServiceStub.queryUserInfo3(UserStr.newBuilder().setStr(str).build()).get().getStr();
            return userResp;
        } catch (InterruptedException | ExecutionException e) {
            System.out.println(e.getCause().getMessage());
        }
        return userResp;
    }

    @ApiOperation("链路监控rpc回调")
    @PostMapping(value = "/callBack")
    public String callBack(@RequestBody String param) {
        log.info("callBack 入参 : {}", param);
        return param + "=== > callBack";
    }

}
